$J(document).ready(function(){ 
	MapList.init();
}); 


function ajax(url,cb){
	$J.get(url,cb);
}

function upDown(mac){
	var btn=$J("#btn_"+mac).text();
	var updown=(btn.indexOf("上升")>-1?0:1);
	
	var url=window.baseUrl+"/api/setLock/"+mac+"/"+updown;
	var cb=function(res){
		if(res.code==200){
			alert("成功");
			$J("#btn_"+mac).text(updown?"上升":"下降");
			$J("#updown_"+mac).text(updown?"降下":"升起");
		}else{
			alert("失败："+res.msg);
		}
	};
	ajax(url,cb);
}

function getState(mac){
	
	var url=window.baseUrl+"/api/getLock/"+mac;
	var cb=function(res){
		if(res.code==200){
			var data=res.data;
			var updown=data.upDownState?"降下":"升起";
			var ele=data.eleState;
			var buzzer=data.buzzerState?"打开":"关闭";
			
			alert("电量等级："+ele+"  升降："+updown+"  蜂鸣器："+buzzer);
			$J("#btn_"+mac).text(data.upDownState?"上升":"下降");
			$J("#updown_"+mac).text(updown);
			$J("#ele_"+mac).text(ele);
			$J("#buzzer_"+mac).text(buzzer);
		}else{
			alert("失败："+res.msg);
		}
	};
	ajax(url,cb);
}

var MapList={
		lockId :null,
		init:function(){
			this.addLisen();
		},
		//删除锁
	  removeObj:function() {
		  var o=this;
			$J.ajax({
				type : 'delete',
				url : window.baseUrl + "/pd/"+o.lockId+"/pdellock" ,
				dataType : 'json',
				async : true,
				cache : false,
				success : function(data) {
					if (data.status == false) {
						if (data.msg)
							$J("#msg").html(data.msg);
						return;
					}
					alert("操作成功！");
					location.reload();
				},
				error : function(data) {
					$J("#msg").html(data.msg);
				}
			});
		},
		//注册事件
		addLisen:function(){
			var o=this;
			$J('body').on('click', 'a[name="remove-content"]', function() {
				o.lockId = $J(this).data('content');
			});
			$J('body').on('click', '#removeBtn', function() {
				o.removeObj();
			});
			$J('body').on('click', '#queryBtn', function() {
				document.queryForm.submit();
			});
		}
}